package com.itboye.ihomebank.zjtlcb.lnd.livenessdetector.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by TLCB on 2018/6/6.
 */
public class MD5Util {
    private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final String SIGN_SHA = "SHA";
    private static final String SIGN_SHA256 = "SHA-256";
    private static final String SIGN_SHA512 = "SHA-512";
    private static final String SIGN_MD5 = "MD5";

    public static String MD5(String key) {
        char hexDigits[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
        try {
            byte[] btInput = key.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static String md5(String str, String signType) {
        MessageDigest sign = null;
        try {
            if (SIGN_MD5.equalsIgnoreCase(signType)) {
                sign = MessageDigest.getInstance(SIGN_MD5);
            } else if (SIGN_SHA.equalsIgnoreCase(signType)) {
                sign = MessageDigest.getInstance(SIGN_SHA);
            } else if (SIGN_SHA256.equalsIgnoreCase(signType)) {
                sign = MessageDigest.getInstance(SIGN_SHA256);
            } else if (SIGN_SHA512.equalsIgnoreCase(signType)) {
                sign = MessageDigest.getInstance(SIGN_SHA512);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (sign != null) {
            try {
                sign.update(str.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            byte[] encodeBytes = sign.digest();
            char encodeStr[] = new char[encodeBytes.length * 2];
            int k = 0;
            for (int i = 0; i < encodeBytes.length; i++) {
                byte encodeByte = encodeBytes[i];
                encodeStr[k++] = hexDigits[encodeByte >> 4 & 0xf];
                encodeStr[k++] = hexDigits[encodeByte & 0xf];
            }
            return new String(encodeStr).toUpperCase();
        }
        return null;
    }

    public static String md5_(String str) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");

            if (md5 != null) {
                md5.update(str.getBytes("utf-8"));
                byte[] encodeBytes = md5.digest();
                char encodeStr[] = new char[encodeBytes.length * 2];
                int k = 0;
                for (int i = 0; i < encodeBytes.length; i++) {
                    byte encodeByte = encodeBytes[i];
                    encodeStr[k++] = hexDigits[encodeByte >> 4 & 0xf];
                    encodeStr[k++] = hexDigits[encodeByte & 0xf];
                }
                return new String(encodeStr).toUpperCase();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
